var gulp = require("gulp");
var cssnano = require("gulp-cssnano");
var rename = require("gulp-rename");
var uglify = require("gulp-uglify");
var cache = require("gulp-cache");
var imagemin = require("gulp-imagemin");
var bs = require("browser-sync").create();
var sass = require("gulp-sass");
var util = require("gulp-util");
var sourcemaps = require("gulp-sourcemaps");

paths = {
	'html': './templates/**/',
	'css': './src/css/**/',
	'js': './src/js/',
	'images': './src/images/',
	'css_dist': './dist/css/',
	'js_dist': './dist/js/',
	'images_dist': './dist/images/'
}

gulp.task('html', function () {
	return gulp.src(paths.html + '*.html')
	//.pipe(bs.stream())
})

gulp.task('css' ,function () {
	return gulp.src(paths.css + '*.scss')
	.pipe(sass().on('error',sass.logError))
	.pipe(cssnano())
	.pipe(rename({
		'suffix': '.min'
	}))
	.pipe(gulp.dest(paths.css_dist))
	//.pipe(bs.stream())
})

gulp.task('js', function () {
	return gulp.src(paths.js + '*.js')
	.pipe(sourcemaps.init())
	.pipe(uglify().on('error',util.log))
	.pipe(rename({
		'suffix': '.min'
	}))
	.pipe(sourcemaps.write())
	.pipe(gulp.dest(paths.js_dist))
	//.pipe(bs.stream())
})

gulp.task('images', function () {
	return gulp.src(paths.images + '*.*')
	.pipe(cache(imagemin()))
	.pipe(gulp.dest(paths.images_dist))
	//.pipe(bs.stream())
})

gulp.task('watch', function () {
	gulp.watch(paths.css + '*.scss', gulp.series('css'));
	gulp.watch(paths.js + '*.js', gulp.series('js'));
	gulp.watch(paths.images + '*.*', gulp.series('images'));
	gulp.watch(paths.html + '*.html', gulp.series('html'));
})

gulp.task('bs', function () {
	return bs.init({
		'server': {
			'baseDir': './'
		}
	})
})

// gulp.task('default', gulp.parallel('bs','watch'));
gulp.task('default', gulp.series('watch'));